<?php

namespace App\Http\Controllers\Api;

use App\Model\Checkgroup;
use App\Model\Commanager;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CompanyRequest;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use App\Model\Comcheckself;
use App\Model\Comcheckselfstep;

use App\Model\Comhealthofficer;
use App\Model\Comsafeofficer;
use App\Model\Comspecialequipmentofficer;
use App\Model\Comspecialworkofficer;
use App\Model\Company;
use App\Model\Comcheck;
use App\Model\Comnature;
use App\Model\Comcheckstep;
use App\Model\Area;
use phpDocumentor\Reflection\Project;
use Carbon\Carbon;
use App\Model\Comcheckdetail;
use App\Model\Comcheckagain;
use App\Model\Comcheckagaindetail;
use App\Model\Checkcategory;
use Illuminate\Support\Facades\DB;

class NinesmallselfController extends Controller
{
    

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 消防非消防
     */
    public function firetype(Request $request)
    {
        $companyId = $request->input('companyId');
        $company = Company::find($companyId);
        $categoryId = $company->categoryId;
//        $request->session()->put('categoryId',$company->categoryId);
        if(!$company) {
            $res_arr["code"] = 400;
            $res_arr["message"] = '公司信息有误';
            return response()->json($res_arr);
        }
        $fire = 0;
        $nofire = 0;
        $zhijian = 0;
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == 1) {
                    $fire += $v->stepStatus;
                }
                if($v->typeStatus == 2) {
                    $nofire += $v->stepStatus;
                }
                if($v->typeStatus == 5) {
                    $zhijian += $v->stepStatus;
                }
            }
        }
        $comcheckInfo = Comcheckself::where(['companyId'=>$companyId])->select('id','status')->first();

        if($comcheckInfo) {
            $checkId = $comcheckInfo['id'];
        } else {
            $user = Auth::user();
            $check['companyId'] = $companyId;
            $check['created'] = date('Y-m-d H:i:s',time());
            $check['userId'] = $user->id;
            $checkId = Comcheckself::insertGetId($check);
        }
//        $request->session()->put('companyId', $companyId);
//        $request->session()->put('checkId', $checkId);
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        $steps = [
            ['title' => '消防','iscomplete'=>$fire,'firetype'=>1,
                'child'=>           [['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>1])->first()->id,
                                    'status' => 0,
                                    'type' => 1,
                                    'title'=>'基础档案'],
                                    ['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>1])->first()->id,
                                        'status' => 0,
                                        'type' => 1,
                                        'title'=>'作业现场'
                                    ]],
            ],
            ['title' => '生产','iscomplete'=>$nofire,'firetype'=>2,
                'child'=>           [['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>2])->first()->id,
                                    'status' => 0,
                                    'type' => 2,
                                    'title'=>'基础档案'],
                                    ['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>2])->first()->id,
                                    'status' => 0,
                                    'type' => 2,
                                    'title'=>'作业现场'
                                    ]],
            ],
            
        ];
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == 1 && $v->stepStatus == 1) {
                    $steps[0]['child'][0]['status'] = 1 ;
                }
                if($v->typeStatus == 1 && $v->stepStatus == 2) {
                    $steps[0]['child'][1]['status'] = 1 ;
                }
                if($v->typeStatus == 2 && $v->stepStatus == 1) {
                    $steps[1]['child'][0]['status'] = 1 ;
                }
                if($v->typeStatus == 2 && $v->stepStatus == 2) {
                    $steps[1]['child'][1]['status'] = 1 ;
                }
            }
        }
        $res['companyId'] = $companyId;
        $res['checkId'] = $checkId;
        $res['categoryId'] = $company->categoryId;
        $res['fire'] = $fire;
        $res['nofire'] = $nofire;
        $res['zhijian'] = $zhijian;
        $res['steps'] = $steps;
        $res_arr["data"] = $res;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * @param Request $request
     * @param int $firetype
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     * 人物环管
     */
    public function choose(Request $request)
    {
        $firetype = $request->input('firetype');
        $companyId = $request->input('companyId');
        $categoryId = $request->input('categoryId');
        if(!$companyId) {
            $res_arr["code"] = 400;
            $res_arr["message"] = '公司信息有误';
            return response()->json($res_arr);
        }
//        $request->session()->put('firetype', $firetype);
        if(!$firetype){
            $res_arr["code"] = 400;
            $res_arr["message"] = '公司信息有误';
            return response()->json($res_arr);
        }
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        $id1 = Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>$firetype])->first();
        $id2 = Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>$firetype])->first();
        $checkId = Comcheckself::where(['companyId'=>$companyId])->first()->id;
        if (empty($id1->id)){
            \Session::flash('error', '检查信息未录入!');
            return back();
        }
        if (empty($id2->id)){
            \Session::flash('error', '检查信息未录入!');
            return back();
        }
        $steps = [
            [
                'id' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>$firetype])->first()->id,
                'status' => 0,
                'title'=>'基础档案'
            ],
            [
                'id' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>$firetype])->first()->id,
                'status' => 0,
                'title'=>'作业现场'
            ],
        ];
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == $firetype) {
                    $steps[$v->stepStatus-1]['status'] = 1 ;
                }
            }
        }
        $res_arr['data']['data'] = $steps;
        $res_arr['data']['companyId'] = $companyId;
        $res_arr['data']['checkId'] = $checkId;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }
    public function companyone(Request $request)
    {
        $id = $request->input('companyId');

//        $user = Auth::user();//获取全部企业
        $companyCom = Company::where(['id'=>$id]);
//        if($user->role == 'admin') {
        $companys = $companyCom->get();
//        } else {
//            $companys = $companyCom->where(['userId'=>$user->id])->get();
//        }
        $type = Auth::user();
        $res_arr['data']['data'] = $companys;
        $res_arr['data']['type'] = $type;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);

    }
    




    public static function getFirstCharter($str)
    {
        if (empty($str)) {
            return '';
        }
        $fchar = ord($str{0});
        if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
        $s1 = iconv('UTF-8', 'gb2312', $str);
        $s2 = iconv('gb2312', 'UTF-8', $s1);
        $s = $s2 == $str ? $s1 : $str;
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if ($asc >= -20319 && $asc <= -20284) return 'a';
        if ($asc >= -20283 && $asc <= -19776) return 'b';
        if ($asc >= -19775 && $asc <= -19219) return 'c';
        if ($asc >= -19218 && $asc <= -18711) return 'd';
        if ($asc >= -18710 && $asc <= -18527) return 'e';
        if ($asc >= -18526 && $asc <= -18240) return 'f';
        if ($asc >= -18239 && $asc <= -17923) return 'g';
        if ($asc >= -17922 && $asc <= -17418) return 'h';
        if ($asc >= -17417 && $asc <= -16475) return 'j';
        if ($asc >= -16474 && $asc <= -16213) return 'k';
        if ($asc >= -16212 && $asc <= -15641) return 'l';
        if ($asc >= -15640 && $asc <= -15166) return 'm';
        if ($asc >= -15165 && $asc <= -14923) return 'n';
        if ($asc >= -14922 && $asc <= -14915) return 'o';
        if ($asc >= -14914 && $asc <= -14631) return 'p';
        if ($asc >= -14630 && $asc <= -14150) return 'q';
        if ($asc >= -14149 && $asc <= -14091) return 'r';
        if ($asc >= -14090 && $asc <= -13319) return 's';
        if ($asc >= -13318 && $asc <= -12839) return 't';
        if ($asc >= -12838 && $asc <= -12557) return 'w';
        if ($asc >= -12556 && $asc <= -11848) return 'x';
        if ($asc >= -11847 && $asc <= -11056) return 'y';
        if ($asc >= -11055 && $asc <= -10247) return 'z';
        return null;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 自查初查接口 消防非消防
     */
    public function firsttype(Request $request)
    {
        //$companyId = $request->input('companyId');
        $user = Auth::user();
        $companyId = $user->companyId;  //当前用户所属企业

        $company = Company::find($companyId);
        $categoryId = $company->categoryId??0;
//        $request->session()->put('categoryId',$company->categoryId);
        if(!$company) {
            $res_arr["code"] = 400;
            $res_arr["message"] = '公司信息有误';
            return response()->json($res_arr);
        }
        $fire = 0;
        $nofire = 0;
        $zhijian = 0;
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == 1) {
                    $fire += $v->stepStatus;
                }
                if($v->typeStatus == 2) {
                    $nofire += $v->stepStatus;
                }
                if($v->typeStatus == 5) {
                    $zhijian += $v->stepStatus;
                }
            }
        }
        $comcheckInfo = Comcheckself::where(['companyId'=>$companyId])->select('id','status')->first();

        if($comcheckInfo) {
            $checkId = $comcheckInfo['id'];
        } else {
            $user = Auth::user();
            $check['companyId'] = $companyId;
            $check['created'] = date('Y-m-d H:i:s',time());
            $check['userId'] = $user->id;
            $checkId = Comcheckself::insertGetId($check);
        }
//        $request->session()->put('companyId', $companyId);
//        $request->session()->put('checkId', $checkId);
        $comcheckstep = new Comcheckselfstep();
        $fireinfo = $comcheckstep->getfiretype($companyId);
        /*$steps = [
            ['title' => '消防','iscomplete'=>$fire,'firetype'=>1,
                'child'=>           [['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>1])->first()->id,
                    'status' => 0,
                    'type' => 1,
                    'title'=>'基础档案'],
                    ['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>1])->first()->id,
                        'status' => 0,
                        'type' => 1,
                        'title'=>'作业现场'
                    ]],
            ],
            ['title' => '生产','iscomplete'=>$nofire,'firetype'=>2,
                'child'=>           [['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>1,'type'=>2])->first()->id,
                    'status' => 0,
                    'type' => 2,
                    'title'=>'基础档案'],
                    ['groupId' => Checkgroup::where(['categoryId'=>$categoryId,'status'=>2,'type'=>2])->first()->id,
                        'status' => 0,
                        'type' => 2,
                        'title'=>'作业现场'
                    ]],
            ],

        ];*/
        //获取类型
        //$getCheckcategory = Checkcategory::find($categoryId);
        $steps = Checkgroup::getCheckgroup($categoryId, $fire, $nofire);
        if(count($fireinfo)){
            foreach ($fireinfo as $k => $v) {
                if($v->typeStatus == 1 && $v->stepStatus == 1) {
                    $steps[0]['child'][0]['status'] = 1 ;
                }
                if($v->typeStatus == 1 && $v->stepStatus == 2) {
                    $steps[0]['child'][1]['status'] = 1 ;
                }
                if($v->typeStatus == 2 && $v->stepStatus == 1) {
                    $steps[1]['child'][0]['status'] = 1 ;
                }
                if($v->typeStatus == 2 && $v->stepStatus == 2) {
                    $steps[1]['child'][1]['status'] = 1 ;
                }
            }
        }
        $res['companyId'] = $companyId;
        $res['checkId'] = $checkId;
        $res['categoryId'] = $company->categoryId;
        $res['fire'] = $fire;
        $res['nofire'] = $nofire;
        $res['zhijian'] = $zhijian;
        $res['steps'] = $steps;
        $res_arr["data"] = $res;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }
}
